//
//  MainClassViewController.m
//  FlyGift
//
//  Created by lanou3g on 15/7/11.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MainClassViewController.h"

@interface MainClassViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)DefaultViewController * defaultVC;
@property(nonatomic,strong)ClassesViewController * classesVC;

@property(nonatomic,strong)UIScrollView * scrollView;

#pragma mark - 增加部分
@property(nonatomic, strong)ListBar *listBar;
@property(nonatomic, strong)DeleteBar *deleteBar;
@property(nonatomic, strong)DetailsList *detailList;
@property(nonatomic, strong)YGArrow *arrow;
@property(nonatomic,strong)UIScrollView *mainScroller;
@property(nonatomic, strong)NSMutableArray *listTop;
@property(nonatomic, strong)NSMutableArray *listBottom;

@end

@implementation MainClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.backgroundColor = KColor(255, 140, 179, 1);
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.listTop = [NSMutableArray arrayWithArray:@[@"精选",@"礼物",@"美食",@"数码",@"运动",@"娱乐",@"穿搭",@"美物",@"鞋包",@"饰品",@"美护"]];
    self.listBottom = [[NSMutableArray alloc] initWithArray:@[@"男票",@"闺蜜",@"生日",@"结婚",@"创意",@"萌萌哒",@"奇葩",@"设计感",@"爸爸妈妈",@"手工"]];
#pragma mark - 修改 :
    //  当前视图控制器的状态栏样式
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [self creatScrollView];
    [self addDefaultViewController];
    [self addSubViewController];
    [self makeContent];
}

#pragma mark --- 添加滚动视图
-(void)creatScrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kP6Height(30), kScreenWidth, kScreenHeight - kP6Height(104))];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.tag = 200;
    self.scrollView.contentSize = CGSizeMake(self.listTop.count * kScreenWidth, kScreenHeight - kP6Height(104));
    [self.view addSubview:_scrollView];
    
}

#pragma mark --- 添加默认显示的视图
-(void)addDefaultViewController
{
    self.defaultVC = [[DefaultViewController alloc]init];
    self.defaultVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kP6Height(150));
    [self addChildViewController:_defaultVC];
    [self.scrollView addSubview:_defaultVC.view];
    self.defaultVC.view.tag = 100;
}
#pragma mark - 添加子视图控制器
-(void)addSubViewController
{
    NSMutableArray *arr = [NSMutableArray arrayWithArray:@[KPresentURL,KCateURL,KDigitURL,KSportURL,KAmusementURL,chuanDaURL,meiWuURL,xieBaoURL,shiPinURL,meiHuURL]];
    for (int i = 1; i < self.listTop.count; i++) {
        self.classesVC = [[ClassesViewController alloc]init];
        self.classesVC.view.frame = CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight - kP6Height(150));
        [self addChildViewController:_classesVC];
        [self.scrollView addSubview:_classesVC.view];
        self.classesVC.view.tag = 100 + i;
        [self.classesVC classTableViewRequestWithUrl:arr[i-1]];
    }
}
#pragma mark - 增添频道部分
- (void)makeContent
{
    __weak typeof(self) unself = self;
    if (!self.detailList)
    {
        self.detailList = [[DetailsList  alloc]initWithFrame:CGRectMake(0, kListBarH - kScreenHeight, kScreenWidth, kScreenHeight - kListBarH)];
        self.detailList.listAll = [NSMutableArray arrayWithObjects:self.listTop,self.listBottom, nil];
        
        self.detailList.longpressedBlock = ^(){
            [unself.deleteBar sortBtnAction:unself.deleteBar.sortBtn];
        };
        // 对频道中的一些添加删除
        self.detailList.operationFormItemBlock = ^(animateType type,NSString *itemName, int index){
            [unself.listBar operationFromBlock:type item:itemName index:index];
        };
        [self.view addSubview:self.detailList];
    }
    if (!self.listBar)
    {
        self.listBar = [[ListBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kListBarH)];
        self.listBar.visibleItemList = self.listTop;
        self.listBar.arrowChange = ^(){
            
            if (unself.arrow.arrowBtnClickBlock)
            {
                unself.arrow.arrowBtnClickBlock();
            }
        };
        self.listBar.listBarItemClickBlock = ^(NSString *itemName,NSInteger itemIndex){
            NSLog(@"点击了按钮");
            [unself.detailList itemRespondFromListBarClickWithItemName:itemName];
            // 移动到该位置
            unself.mainScroller.contentOffset = CGPointMake(itemIndex * unself.mainScroller.frame.size.width, 0);
            // 将大的scrollView移动到对应的位置
            unself.scrollView.contentOffset = CGPointMake(kScreenWidth * itemIndex, 0);
        };
        [self.view addSubview:_listBar];
    }
    if (!self.deleteBar)
    {
        self.deleteBar = [[DeleteBar alloc]initWithFrame:self.listBar.frame];
        [self.view addSubview:self.deleteBar];
    }
    if (!self.arrow) {
        self.arrow = [[YGArrow alloc] initWithFrame:CGRectMake(kScreenWidth-kArrowW, 0, kArrowW, kListBarH)];
        self.arrow.arrowBtnClickBlock = ^(){
            unself.deleteBar.hidden = !unself.deleteBar.hidden;
            [UIView animateWithDuration:kAnimationTime animations:^{
                CGAffineTransform rotation = unself.arrow.imageView.transform;
                unself.arrow.imageView.transform = CGAffineTransformRotate(rotation,M_PI);
                unself.detailList.transform = (unself.detailList.frame.origin.y<0)?CGAffineTransformMakeTranslation(0, kScreenHeight):CGAffineTransformMakeTranslation(0, -kScreenHeight);
            }];
        };
        [self.view addSubview:self.arrow];
        if (!self.mainScroller) {
            self.mainScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kListBarH, kScreenWidth , kScreenHeight-kListBarH-64)];
            self.mainScroller.bounces = NO;
            self.mainScroller.pagingEnabled = YES;
            self.mainScroller.showsHorizontalScrollIndicator = NO;
            self.mainScroller.showsVerticalScrollIndicator = NO;
            self.mainScroller.delegate = self;
            self.mainScroller.contentSize = CGSizeMake(kScreenWidth*10,self.mainScroller.frame.size.height);
            [self.view insertSubview:self.mainScroller atIndex:0];
        }
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self.listBar itemClickByScrollerWithIndex:scrollView.contentOffset.x / self.mainScroller.frame.size.width];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
